package com.wzl.backend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzl.backend.mapper.TopicMapper;
import com.wzl.backend.pojo.Topic;
import com.wzl.backend.pojo.User;
import com.wzl.backend.service.TopicService;
import com.wzl.backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper , Topic>
                                implements TopicService {

    @Autowired
    UserService userService;

    @Transactional
    @Override
    public boolean insert(Topic topic) {
        Date date = new Date(System.currentTimeMillis());
        topic.setCreateTime(date);
        topic.setLikes(0);
        return save(topic);
    }

    @Override
    public List<Map<String , Object>> getAll() {
        List<Map<String , Object>> maps = new ArrayList<>();
        list().forEach(e -> {
            Map<String , Object> map = new HashMap<>();
            User byId = userService.getById(e.getUserId());
            map.put("topic" , e);
            map.put("user" , byId);
            maps.add(map);
        });
        return maps;
    }
}
